<?php

/*
 * This file is part of DataCenter.
 *
 * (c) Deloz <npmbot@gmail.com>
 *
 * This source file is subject to the WTF license that is bundled
 * with this source code in the file LICENSE.
 */

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
//use yii2tech\ar\softdelete\SoftDeleteBehavior;

class BaseActiveRecord extends ActiveRecord
{
    // 是否删除:否
    const IS_DELETED_NO = 0;
    // 是否删除:是
    const IS_DELETED_YES = 1;
    public $debug = false;

    public function init()
    {
        parent::init();
        $this->debug = YII_DEBUG;
    }

    public function behaviors()
    {
        return [
//            [
//                'class' => TimestampBehavior::className(),
//                'attributes' => [
//                    ActiveRecord::EVENT_BEFORE_INSERT => ['created', 'updated'],
//                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated'],
//                ],
//            ],
//            [
//                'class' => SoftDeleteBehavior::className(),
//                'softDeleteAttributeValues' => [
//                    'deleted_at' => function () {
//                        return time();
//                    },
//                    'is_deleted' => true,
//                ],
//            ],
        ];
    }

    /**
     * 读取第一条错误信息.
     *
     * @param null $attribute
     *
     * @return mixed|string
     */
    public function getFirstError($attribute = null)
    {
        if (null === $attribute) {
            $firstErrors = parent::getFirstErrors();

            return reset($firstErrors);
        }

        return parent::getFirstError($attribute);
    }

    public static function items()
    {
        return [];
    }

    public static function itemAlias(string $type, $code = null)
    {
        $items = static::items();
        if (isset($code)) {
            if (array_key_exists($type, $items)) {
                $data = $items[$type];
                $code = trim($code);

                return array_key_exists($code, $data) ? $data[$code] : null;
            }

            return null;
        }

        return array_key_exists($type, $items) ? $items[$type] : null;
    }

    public static function ruleInRange(string $name)
    {
        return array_keys(self::itemAlias($name));
    }

    public function createdAtTitle(): string
    {
        return date('Y-m-d H:i:s', $this->created_at);
    }

    /**
     * 检查是否「已删除」.
     *
     * @return bool true已删除，false未删除
     */
    public function isDeleted(): bool
    {
        return $this->is_deleted === self::IS_DELETED_YES;
    }

    /**
     * @param int $id
     *
     * @return null|ActiveRecord|array
     */
    public static function findById(int $id)
    {
        return static::find()->andWhere([
            'id' => $id,
        ])->limit(1)
            ->one();
    }

    /**
     * @param int $id
     *
     * @return mixed
     */
    public static function findNotDeletedById(int $id)
    {
        return static::find()->notDeleted()
            ->andWhere([
                'id' => $id,
            ])->limit(1)
            ->one();
    }
}
